package com.qf.sy2103.ssm.controller;

import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.service.UserServcie;
import com.qf.sy2103.ssm.vo.UserVo;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServcie userServcie;
    // http://localhost:8080/user/login
    @RequestMapping("/login")
    public String  login(String name,String password,HttpServletRequest req,HttpSession session){
        User sname = userServcie.sname(name);
        if (Objects.isNull(name)){
            req.setAttribute("msg","用户名不存在");
        }
        User login = userServcie.login(name, password);
        if (Objects.nonNull(login)){
            session.setAttribute("userlogin",login);
            return "redirect:/cha.jsp";
        }
        else{
            req.setAttribute("msg","用户名密码错误");
        }
        return "forward:/cha.jsp";
    }
    // http://localhost:8080/user/regist'
    @RequestMapping(value = "/regist")
    public String Regist(UserVo userVo, MultipartFile src, HttpServletRequest req,HttpSession session) throws IOException {
        User sname = userServcie.sname(userVo.getName());
        String filename = src.getOriginalFilename();

        String unique = UUID.randomUUID().toString();

        String ext = FilenameUtils.getExtension(filename);
        String fileName = unique+"."+ext;
        String real_path = session.getServletContext().getRealPath("/upload_file");
        File file = new File(real_path);
        if (!file.exists()) {
            file.mkdirs();
        }
        src.transferTo(new File(real_path+"\\"+fileName));

        if (Objects.isNull(sname)){
            User user = new User(userVo.getName(), userVo.getPassword(), userVo.getPhone(),fileName);
            int regist = userServcie.regist(user);
            return "redirect:index";
        }
        else {
            req.setAttribute("msg","用户名已存在");
        }
        return "forward:/regist.jsp";

    }
    // http://localhost:8080/user/select
    @RequestMapping("/select")
    public String SName(String name, HttpServletRequest req, Model model){
        User sname = userServcie.sname(name);
        if (Objects.nonNull(sname)){
           model.addAttribute("user",sname);
            return "forward:/cha.jsp";
        }
            req.setAttribute("msg","查无此人");
            return "forward:/cha.jsp";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("userlogin",null);
        return "redirect:index";
    }

    @RequestMapping("/img")
    public void img(HttpServletResponse response,HttpServletRequest request,String  name) throws IOException {
        String realPath = request.getServletContext().getRealPath("/upload_file/" + name);
        IOUtils.copy(new FileInputStream(new File(realPath)),response.getOutputStream());
    }

}
